<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href='/css/styles.css' rel='stylesheet' type='text/css' />
    <link href='/images/favicon.png' rel='shortcut icon' />
    <script src='/js/jquery.min.1.4.js'></script>
    <script src='/js/app.js'></script>
    <script src='/js/common.js'></script>
    
    <meta content='width=device-width, minimum-scale=1.0, maximum-scale=1.0' name='viewport' />
    <title>redis - 命令</title>
	<meta http-equiv="description" content="redis中文资料站，下载安装redis，查找redis常用命令（commands），选择适合的redis客户端方式，配置redis主从（master-slave），阅读redis官方文档，社区里了解更多redis信息，提交redis的bug。" />
	
  </head>
  <body class=''>
    <script src='/js/head.js'></script>
    <div class='text'>
      <h1 class='command'>
        <span id='command_name_span' class='name'></span>
        <span id='command_args_span' class='arg'></span>
      </h1>
      <article>
      	<aside>
        	<script type='text/javascript'>showCmdURL();</script>
        </aside>
        
        <div class='metadata'>
          <p><strong>加入版本 <span id='command_ver_span'></span>。</strong></p>
          <p><strong>时间复杂度：</strong> O(N)。</p>
        </div>
        
        <p>Perform a bitwise operation between multiple keys (containing string values) and
        store the result in the destination key.</p>
        
        <p>The <a href="/commands/bitop.html">BITOP</a> command supports four bitwise operations: <strong>AND</strong>, <strong>OR</strong>, <strong>XOR</strong>
        and <strong>NOT</strong>, thus the valid forms to call the command are:</p>
        
        <ul>
        <li>BITOP AND <em>destkey srckey1 srckey2 srckey3 ... srckeyN</em></li>
        <li>BITOP OR <em>destkey srckey1 srckey2 srckey3 ... srckeyN</em></li>
        <li>BITOP XOR <em>destkey srckey1 srckey2 srckey3 ... srckeyN</em></li>
        <li>BITOP NOT <em>destkey srckey</em></li>
        </ul>
        
        <p>As you can see <strong>NOT</strong> is special as it only takes an input key, because it
        performs inversion of bits so it only makes sense as an unary operator.</p>
        
        <p>The result of the operation is always stored at <em>destkey</em>.</p>
        
        <h2>Handling of strings with different lengths</h2>
        
        <p>When an operation is performed between strings having different lengths, all the
        strings shorter than the longest string in the set are treated as if they were
        zero-padded up to the length of the longest string.</p>
        
        <p>The same holds true for non-existent keys, that are considered as a stream of
        zero bytes up to the length of the longest string.</p>
        
        <h2>Return value</h2>
        
        <p><a href="/topics/protocol.html#integer-reply">Integer reply</a></p>
        
        <p>The size of the string stored in the destination key, that is equal to the
        size of the longest input string.</p>
        
        <h2>Examples</h2>
        
        <div class='example' data-session='f445b1f73ecb0481f557d8cb3d6080a0'>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>SET key1 &quot;foobar&quot;</span>
          <pre>OK</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>SET key2 &quot;abcdef&quot;</span>
          <pre>OK</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>BITOP AND dest key1 key2</span>
          <pre>ERR Don't know what to do for "bitop"</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>GET dest</span>
          <pre>(nil)</pre><form>
            <span class='monospace prompt'>redis&gt;&nbsp;</span>
            <input autocomplete='off' name='command' spellcheck='false' type='text' />
          </form></div>
        
        
        <h2>Pattern: real time metrics using bitmaps</h2>
        
        <p><a href="/commands/bitop.html">BITOP</a> is a good complement to the pattern documented in the <a href="/commands/bitcount.html">BITCOUNT</a> command
        documentation.
        Different bitmaps can be combined in order to obtain a target bitmap where
        the population counting operation is performed.</p>
        
        <p>See the article called &quot;<a href="http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps">Fast easy realtime metrics using Redis
        bitmaps</a>&quot; for a interesting use cases.</p>
        
        <h2>Performance considerations</h2>
        
        <p><a href="/commands/bitop.html">BITOP</a> is a potentially slow command as it runs in <span class="math">O(N) </span>time.
        Care should be taken when running it against long input strings.</p>
        
        <p>For real-time metrics and statistics involving large inputs a good approach is
        to use a slave (with read-only option disabled) where the bit-wise
        operations are performed to avoid blocking the master instance.</p>
        
      </article>
    </div>
    <script type='text/javascript'>startShow();</script>
    <div class='text' id='comments'>
      <div id='disqus_thread'></div>
      <script type='text/javascript'>
        //<![CDATA[
          var disqus_shortname = 'rediscn';
          
          // The following are highly recommended additional parameters. Remove the slashes in front to use.
          var disqus_identifier = 'command_'+curCommandObj.key;
          var disqus_url = curCommandObj.getdisqusUrl();  // = 'http://redis.cn/commands/'+curCommandObj.key + ".html";
          
          /* * * DON'T EDIT BELOW THIS LINE * * */
          (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
              dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
              (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
          })();
        //]]>
      </script>
      <a class='dsq-brlink' href='http://disqus.com'>
        Comments powered by
        <span class='logo-disqus'>
          Disqus
        </span>
      </a>
    </div>

    
    <script src='/js/foot.js'></script>
    
  </body>
</html>
